import { login } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

export default {
  namespaced: true,
  state() {
    return {
      token: getToken(),
      userInfo: {},
      data: {}
    }
  },
  mutations: {
    updataToken(state, payload) {
      state.token = payload.token
      state.data = payload
      setToken(payload.token)
    },
    delToken(state, payload) {
      state.token = null
      removeToken()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    async storeLogin(context, payload) {
      // 发起请求
      const data = await login(payload)
      // 修改token的方法

      if (data.success) {
        context.commit('updataToken', data)
      }
    },
    // async StoreGetUser(textCont) {
    //   const data = await getInfo()
    //   console.log(data)
    //   textCont.commit('setUserInfo', data)
    // },
    StoreLogout(textCont) {
      textCont.commit('delToken')
      textCont.commit('setUserInfo', {})
    }
  }
}
